﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using UI;

namespace UIWPF
{
    /// <summary>
    /// Interaction logic for CodeCompareWindow.xaml
    /// </summary>
    public partial class CodeCompareWindow : Window
    {
        private bool _tmFlag = true;
        private bool _doHiding = false;
        private DispatcherTimer tm = new DispatcherTimer();
        private bool m_AsmAlreadyGenerated = false;

        public CodeCompareWindow()
        {
            InitializeComponent();

            tm.Tick += TickGradual;
            tm.Interval = TimeSpan.FromSeconds(0.01);
        }

        private void TickGradual(object sender, EventArgs e)
        {
            if (_tmFlag)
            {
                Opacity += 0.02;
                if (Opacity >= 1.0)
                {
                    tm.Stop();
                }
            }
            else
            {
                Opacity -= 0.02;
                if (Opacity <= 0.0)
                {
                    if (_doHiding)
                    {
                        tm.Stop();
                        Hide();
                    }
                    else
                    {
                        Close();
                    }
                }
            }
        }

        private void doOpening()
        {
            Show();
            Opacity = 0.0;
            _tmFlag = true;
            tm.Start();
        }
        private void doClosing()
        {
            _doHiding = false;
            _tmFlag = false;
            tm.Start();
        }
        private void doHiding()
        {
            _doHiding = true;
            _tmFlag = false;
            tm.Start();
        }

        private void BtnExitClick(object sender, RoutedEventArgs e)
        {
            doClosing();
        }

        private void DragWindowMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void FormLoading(object sender, RoutedEventArgs e)
        {
            RichTextBoxUtility.RtbAppendStr(rtbHint, "Welcome.\n\n");
            RichTextBoxUtility.RtbAppendStr(rtbHint, "Paste the codes into two compare box.\n");
            RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button TextDiff: Compare the text with the LCS algorithm.\n");
            RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button ListDiff: Separate the text into words and compare with the LCS algorithm.\n");
            RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button AsmCompare: Generate the asm codes and compare the instructions.\n");
            Opacity = 0;
            doOpening();
        }

        private void BtnTestClick(object sender, RoutedEventArgs e)
        {
            UI.Form1 form1 = new Form1();
            form1.Show();
//             MyMessageBox.Show("Title", "Content", false);
//             RichTextBoxUtility.RtbAppendStr(rtbHint, "Welcome.\n");
//             RichTextBoxUtility.RtbAppendStr(rtbHint, "Paste the codes into two compare box.\n");
//             RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button TextDiff: Compare the text with the LCS algorithm.\n");
//             RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button ListDiff: Separate the text into words and compare with the LCS algorithm.\n");
//             RichTextBoxUtility.RtbAppendStr(rtbHint, "  *--Button AsmCompare: Generate the asm codes and compare the instructions.\n");
//             Utility.Colorize(rtbCompareBox1, 0, 2, Color.FromArgb(255, 255, 0, 0));
//             Utility.Colorize(rtbCompareBox1, 4, 2, Color.FromArgb(255, 255, 0, 0));
//             Utility.Colorize(rtbCompareBox1, 8, 4, Color.FromArgb(255, 255, 0, 0));
//             Utility.Colorize(rtbCompareBox1, 15, 3, Color.FromArgb(255, 255, 0, 0));
//             Utility.Colorize(rtbCompareBox1, 20, 5, Color.FromArgb(255, 255, 0, 0));
        }

        private void BtnTextDiffClick(object sender, RoutedEventArgs e)
        {
            CompareManager.TextCompareBegin(rtbCompareBox1, rtbCompareBox2, rtbHint, cbColorize.IsChecked);
        }

        private void BtnListDiffClick(object sender, RoutedEventArgs e)
        {
            CompareManager.ListCompareBegin(rtbCompareBox1, rtbCompareBox2, rtbHint, cbColorize.IsChecked);
        }

        private void BtnAsmCompareClick(object sender, RoutedEventArgs e)
        {
            CompareManager.AsmCompare(rtbCompareBox1, rtbCompareBox2, rtbHint, cbColorize.IsChecked);
            m_AsmAlreadyGenerated = true;
//             btnShowCppCode.IsEnabled = true;
//             btnShowCppCode.Content = "CppCode";
        }

        private void SetLineHeight(object sender, RoutedEventArgs e)
        {
            rtbCompareBox1.Document.LineHeight = 1;
            rtbCompareBox2.Document.LineHeight = 1;
        }

        private void BtnShowCppClick(object sender, RoutedEventArgs e)
        {
            if (!m_AsmAlreadyGenerated)
            {
                MyMessageBox.Show("ASM codes haven't been generated!");
            }
            else
            {
                string fileName1 = "RichTextBox1.cpp", fileName2 = "RichTextBox2.cpp";
//                 if (btnShowCppCode.Content == "AsmCode")
//                 {
//                     fileName1 += ".asm";
//                     fileName2 += ".asm";
//                     btnShowCppCode.Content = "CppCode";
//                 }
//                 else
//                 {
//                     btnShowCppCode.Content = "AsmCode";
//                 }

                string content1, content2;
                FileStream fs1 = new FileStream(fileName1, FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                content1 = sr1.ReadToEnd();
                sr1.Close();
                fs1.Close();

                FileStream fs2 = new FileStream(fileName2, FileMode.Open);
                StreamReader sr2 = new StreamReader(fs2);
                content2 = sr2.ReadToEnd();
                sr2.Close();
                fs2.Close();

                RichTextBoxUtility.SetRichTextBoxContent(rtbCompareBox1, content1);
                RichTextBoxUtility.SetRichTextBoxContent(rtbCompareBox2, content2);

//                 btnShowCppCode.IsEnabled = true;
                m_AsmAlreadyGenerated = true;
            }
        }

        // problem
        private void rtbTextChanged(object sender, TextChangedEventArgs e)
        {
//             if (btnShowCppCode != null)
//             {
//                 btnShowCppCode.IsEnabled = false;
//             }
            m_AsmAlreadyGenerated = false;
        }
    }
}
